package com.shizhanzhe.zxbapp.customtask;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.Publish;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/12/26.
 */
public class PublishAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private List<Publish> list;
    private Context context;

    public PublishAdapter(Context context, List<Publish> list) {
        this.list=list;
        this.context=context;
        inflater = LayoutInflater.from(context);
    }
    public void setImgList(List<LocalMedia> list){
        adapter.setList(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHold hold;
        if (view==null){
            view=inflater.inflate(R.layout.item_publish,null);
            hold=new ViewHold();
            hold.count=view.findViewById(R.id.tv_step);
            hold.content=view.findViewById(R.id.tv_step_name);
            hold.recycler=view.findViewById(R.id.recycler);
            hold.delete=view.findViewById(R.id.delete);
            view.setTag(hold);
        }else {
            hold= (ViewHold) view.getTag();
        }
        final Publish bean = list.get(i);
        hold.count.setText("第"+(i+1)+"步");
        hold.content.setTag(i);
        hold.content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if ((int)hold.content.getTag()==i) {
                    if ("".equals(editable.toString())){
                        bean.setContent("");
                    }else {
                        bean.setContent(editable.toString());
                    }
                }
            }
        });
        GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                // 进入相册 以下是例子：不需要的api可以不写
                PictureSelector.create((Activity) context)
                        .openGallery(PictureMimeType.ofAll())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                        .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style
                        .maxSelectNum(maxSelectNum - bean.getSelectList().size())// 最大图片选择数量
                        .minSelectNum(1)// 最小选择数量
                        .imageSpanCount(3)// 每行显示个数
                        .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                        .selectionMedia(bean.getSelectList())// 是否传入已选图片
                        .forResult(i);//结果回调onActivityResult code

            }

        };
        FullyGridLayoutManager manager = new FullyGridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
        hold.recycler.setLayoutManager(manager);
        adapter = new GridImageAdapter(context, onAddPicClickListener);
        adapter.setList(bean.getSelectList());
        adapter.setSelectMax(maxSelectNum);
        hold.recycler.setAdapter(adapter);
        hold.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(i);
                notifyDataSetChanged();
            }
        });
        return view;
    }
    class ViewHold{
        TextView count;
        EditText content;
        RecyclerView recycler;
        ImageView delete;
    }
    private GridImageAdapter adapter;
    private int maxSelectNum = 3;

}
